<?php
/**
 * Created by PhpStorm.
 * User: lcz
 * Date: 2018/6/5
 * Time: 11:17
 * 反馈意见
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\WebMail;
use think\Db;

class Feedback extends Api {
	protected $noNeedRight = ['*'];
	
	/**
	 * 添加反馈
	 * @param token
	 * @param content 反馈意见
	 */
	public function add(){
		$userId = $this->auth->id;
		$content = input('post.content');
		!$content && $this->error('请填写反馈意见');
		
		$model = new \app\common\model\Feedback();
		$res = $model->isUpdate(false)->save([
			'user_id' => $userId,
			'content' => $content,
		]);
		
		if($res) {
			$this->success('提交成功');
		} else {
			$this->error('添加出错，请稍候再试');
		}
	}
	
	/**
	 * 我的反馈记录
	 * @param token
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function index(){
		$userId = $this->auth->id;
		
		$model = new \app\common\model\Feedback();
		
		$rows = Db::name('feedback')->where(['user_id' => $userId])
			->field('id, content, from_unixtime(create_time) as create_time, reply')
			->order(['create_time' => 'DESC'])
			->select();
		$this->success('查询成功', $rows);
	}
	
	/**
	 * 站内信
	 */
	public function webmail(){
		$rows = Db::name('webMail')
			->where(['user_id' => $this->auth->id])
			->field('id, info, from_unixtime(create_time) as create_time')
			->order(['create_time' => 'desc'])
			->page(input('post.page', 1, 'intval'))
			->select();
		
		Db::name('webMail')->where(['user_id' => $this->auth->id])
			->setField('status', WebMail::STATUS_Y);
		
		$this->success('查询成功', $rows);
	}
}
